import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

//实现一个固定线程个数的线程池
class MyThreadPool {
    BlockingQueue<Runnable> queue= null;
    public MyThreadPool(int n){
        //初始化线程池创建出固定个数的线程
        queue=new ArrayBlockingQueue<>(1000);
        for (int i = 0; i < n; i++) {
            Thread t=new Thread(()->{
                try {
                   while (true){
                       Runnable task= queue.take();
                       task.run();
                   }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
            t.start();
        }
    }
    public void submit(Runnable task){
        try {
            queue.put(task);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

public class Demo4 {
    public static void main(String[] args) {
        MyThreadPool threadPool=new MyThreadPool(10);
        for (int i = 0; i < 1000; i++) {
           int id=i;
           threadPool.submit(()->{
               System.out.println(Thread.currentThread().getName()+" i= "+id);
           });
        }
    }
}
